package com.tbit.uqbike.client.controller;

import com.tbit.uqbike.client.constant.ResponseCode;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.Park;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.service.MachineService;
import com.tbit.uqbike.client.service.ParkService;
import com.tbit.uqbike.client.service.TokenService;
import com.tbit.uqbike.client.util.OutputUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

/**
 * 中途停车
 *
 * @author Leon
 * 2017年2月24日 下午5:51:05
 */
@Controller
@RequestMapping("/park")
public class ParkController {
    @Autowired
    private ParkService parkService;
    @Autowired
    private MachineService machineService;
    @Autowired
    private TokenService tokenService;


    @GetMapping("/machinePositionCheck")
    public void machineInSite(String token, Double phonePositionLon, Double phonePositionLat, Integer type,
                              HttpServletRequest request, HttpServletResponse response) {

        Map<String, Object> resultMap = new HashMap<>();
        try {
            User user = tokenService.getUser(token);
            Boolean result = parkService.machinePositionCheck(user.getUserId(), type, phonePositionLon, phonePositionLat);
            resultMap.put(ResponseCode.RET, ResponseCode.SUCC);
            resultMap.put(ResponseCode.DATA, result);
        } catch (BaseException e) {
            ResponseCode.printException(resultMap, e, request);
        }
        OutputUtil.renderText(request, response, resultMap);
    }

    /**
     * OutputUtil.renderText(request, response, map);
     * 中途停车
     *
     * @param token            令牌
     * @param ble              是否蓝牙
     * @param response         响应
     * @param phonePositionLon 手机经度
     * @param phonePositionLat 手机纬度
     * @ignoreParams request
     */
    @RequestMapping("/parkRide")
    public void borrowBike(String token, @RequestParam(required = false, defaultValue = "false") Boolean ble,
                           Double phonePositionLon, Double phonePositionLat,
                           HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<>();

        try {
            User user = tokenService.getUser(token);
            //默认来源微信小程序
            int orderSource = Optional.ofNullable(request.getHeader("orderSource"))
                    .filter(StringUtils::isNotBlank)
                    .map(Integer::parseInt)
                    .orElse(3);
            //生成流水号
            String serNo = UUID.randomUUID().toString();
            parkService.parkRide(serNo, user.getUserId(), ble, orderSource, phonePositionLon, phonePositionLat);
            //蓝牙通道，上锁
            if (Boolean.TRUE.equals(ble)) {
                machineService.ter_lock(serNo);
            }
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, serNo);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }
        OutputUtil.renderText(request, response, map);
    }


    /**
     * 继续骑行
     *
     * @param token    令牌
     * @param ble      是否蓝牙
     * @param response 响应
     */
    @RequestMapping("/continueRide")
    public void continueRide(String token, @RequestParam(required = false, defaultValue = "false") Boolean ble,
                             HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<>();

        try {
            User user = tokenService.getUser(token);
            //默认来源微信小程序
            int orderSource = Optional.ofNullable(request.getHeader("orderSource"))
                    .filter(StringUtils::isNotBlank)
                    .map(Integer::parseInt)
                    .orElse(3);
            //生成流水号
            String serNo = UUID.randomUUID().toString();
            parkService.continueRide(serNo, user.getUserId(), ble, orderSource);
            //蓝牙通道，继续骑行
            if (Boolean.TRUE.equals(ble)) {
                machineService.ter_unlock(serNo);
            }
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, serNo);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }
        OutputUtil.renderText(request, response, map);
    }


    /**
     * 查询中途停车记录
     *
     * @param token    令牌
     * @param session  会话
     * @param response 响应
     * @ignoreParams request
     */
    @RequestMapping("/getByUserId")
    public void getByUserId(String token, HttpSession session, HttpServletRequest request,
                            HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            User user = tokenService.getUser(token);

            Park park = parkService.getParking(user.getUserId());

            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, park);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }


}